<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/3 0003
 * Time: 14:54
 */

namespace App\Models\ActivityDelivery;

use App\Models\ActivityDeliveryDetail\ActivityDeliveryDetail;
use App\Models\ActivityPoints\ActivityPoints;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ActivityDeliveryRepository implements \App\Contracts\Repositories\ActivityDeliveryRepository
{
    public function create(ActivityDelivery $activityDelivery)
    {
        if (!is_null($activityDelivery->id)) {
            throw new \Exception('duplicate id');
        }
        if ($activityDelivery->save()) {
            return $activityDelivery;
        }

        return null;
    }

    public function findByConditionsPage($condition, $sortArray = [], $start = 0, $limit = 10)
    {
        $qb = ActivityDelivery::with('ActivityPoints')->where($condition)->limit($limit)->skip($start);
        if (!empty($sortArray)) {
            foreach ($sortArray as $sort) {
                $qb = $qb->orderBy($sort[0], $sort[1]);
            }
        }

        return $qb->get();
    }

    public function countByConditions($condition)
    {
        return ActivityDelivery::with('ActivityPoints')->where($condition)->count();
    }

    public function deleteByIds($ids)
    {
        return ActivityDelivery::destroy($ids);
    }

    public function getActivityDeliveryById($id)
    {
        return ActivityDelivery::query()->find($id);
    }

    public function update($activityDelivery, $conditions)
    {
        return ActivityDelivery::query()->where($conditions)->update($activityDelivery);
    }

    public function getActivityDeliveryAndDetailsById($id)
    {
        return ActivityDelivery::query()->where(['id' => $id])->get()->toArray();
    }

    public function createDeliveryAndDetail($activityDeliveryData)
    {
        $activityDelivery = new ActivityDelivery($activityDeliveryData);
        $model = $this->create($activityDelivery);
        if ($model->id) {
            return true;
        }
        return false;
    }

    public function modifyDeliveryAndDetail($activityDeliveryData, $id)
    {
        $activityDelivery = $this->update($activityDeliveryData, ['id' => $id]);
        if ($activityDelivery) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查询可用的埋点
     * @return array
     */
    public function getAvailablePoints($point_id='')
    {
        $time = time();
        $res = DB::table('activity_deliveries as a')
            ->select('point_id')
            ->where("begin_time", "<", $time)->where("end_time", ">", $time)
            ->get()->toArray();
        $point_ids = [];
        if ($res) {
            foreach ($res as $key => $val) {
                if ($val->point_id == $point_id) {
                    continue;
                }
                array_push($point_ids, $val->point_id);
            }
        }
        $data = ActivityPoints::query()->whereNotIn('id', $point_ids)->get()->toArray();

        return $data;
    }
}
